//###############
//FILE : FileWantAllFilesSession.java
//WRITER : Daniel Lowengrub, daniel_i_l, 320687098
//EXERCISE : oop ex3 2011
//DESCRIPTION:
//See below
//###############

package oop.ex3.protocol.sessions;

import oop.ex3.protocol.messages.NsContainFileMsg;
import oop.ex3.protocol.messages.WantAllFilesMsg;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import oop.ex3.exceptions.InvalidMessageException;

/**
 * Ask a NS for a list of the files it knows about.
 * @author daniel
 */

public class FileWantAllFilesSession extends FileToNameSession {
    private ArrayList<String> _fileNameOutput;

    /**
	 * Create a new session.
	 */
    public FileWantAllFilesSession() {
        super();
        _fileNameOutput = new ArrayList<String>();
    }

    /**
     * Get a list of the file names we got from the NS.
     * @return
     */
    public ArrayList<String> getFileNames() {
        return _fileNameOutput;
    }

    @Override
    protected boolean specExecuteSession(DataInputStream inputStream, DataOutputStream outputStream) throws IOException, InvalidMessageException {

        if (!initSession(inputStream, outputStream))
            return false;

        WantAllFilesMsg wantFiles = new WantAllFilesMsg();
        wantFiles.send(outputStream);

        //receive the list of nameservers
        while (!_endList.matchHeader(inputStream)) {
            NsContainFileMsg containFile = new NsContainFileMsg();
            containFile.receive(inputStream);
            _fileNameOutput.add(containFile.getFileName());
        }

        _endList.receive(inputStream);

        if (!endSession(inputStream, outputStream))
            return false;

        return true;
    }




}
